package com.lovo.roomProject.dao;

import java.sql.*;

public abstract class BaseDAO {
    protected Connection con;
    protected PreparedStatement statement;
    protected ResultSet rs;
    protected boolean autoCommit = true;

    public Connection getConnection() {
        try {
            if (con == null || con.isClosed()) {
                // 1. 加载驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
                // 2. 创建连接
                con = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/room?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                        "root", "z6787526");
            }
        } catch (ClassNotFoundException | SQLSyntaxErrorException | NullPointerException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return con;
    }

    //关闭连接
    public void close(PreparedStatement statement, Connection con) {
        //4.关闭连接
        if (statement != null && autoCommit) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null && autoCommit) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //关闭连接
    public void close() {
        //4.关闭连接
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void close(ResultSet rs, PreparedStatement statement, Connection con) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null && autoCommit) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null && autoCommit) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void setAutoCommit(boolean autoCommit) {
        try {
            this.autoCommit = autoCommit;
            this.getConnection();
            con.setAutoCommit(autoCommit);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void commit() {
        try {
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void rollback() {
        try {
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

